package moni.anyou.com.view.view;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.ext.message.TIMMessageDraft;
import com.tencent.imsdk.ext.message.TIMMessageLocator;
import com.tencent.qcloud.presentation.event.MessageEvent;
import com.tencent.qcloud.presentation.presenter.ChatPresenter;
import com.tencent.qcloud.presentation.viewfeatures.ChatView;
import com.tencent.qcloud.timchat.model.Message;
import com.tencent.qcloud.timchat.model.TextMessage;
import com.tencent.qcloud.ui.ChatInput;
import com.tencent.qcloud.ui.ChatInput.ChatInfoBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import moni.anyou.com.view.R;
import moni.anyou.com.view.config.SysConfig;
import moni.anyou.com.view.widget.utils.imageload.ImageLoadUtil;


/**
 * Created by Lin on 16/8/29.
 */

public class ConfirmChatDialogActivity extends AppCompatActivity implements ChatView {


    @BindView(R.id.cv_head)
    CircleImageView cvHead;
    @BindView(R.id.tv_msg)
    TextView tvMsg;
    @BindView(R.id.iv_endcall)
    ImageView ivEndcall;
    @BindView(R.id.iv_check)
    ImageView ivCheck;
    @BindView(R.id.activity_voice_chat_view)
    RelativeLayout activityVoiceChatView;

    @BindView(R.id.ll_check)
    LinearLayout llCheck;

    private String info;
    private ChatInfoBean chatInfiBean;
    private int chatType;


    public final int CHAT_TYPE_VOICE = 0;
    public final int CHAT_TYPE_VIDEO = 1;

    ChatPresenter presenter;

    public boolean isAnswer=false;

    public boolean isFinish=false;


    //接收音视频消息
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void subscribe(ChatInfoBean bean) {

       Log.d("msg","eventbus->"+bean.streamType);

        if(ChatInput.streamChatClose.equals(bean.streamType))
        {
            isFinish=true;
            finish();
        }else if(ChatInput.streamChatAnswer.equals(bean.streamType)){
            isFinish=true;
            finish();

//            Intent intent = null;
//            if (chatType == CHAT_TYPE_VOICE) {
//                intent = new Intent(ConfirmChatDialogActivity.this, VoiceChatViewActivity.class);
//
//            } else {
//                intent = new Intent(ConfirmChatDialogActivity.this, VideoChatViewActivity.class);
//            }
//
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.putExtra("chat", info);
//
//            startActivity(intent);
//
//            finish();


        }

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_chat_info_wait);
        ButterKnife.bind(this);


        this.setFinishOnTouchOutside(false);

        info = getIntent().getStringExtra("chat");


        Log.e("test",info);


        chatInfiBean = new Gson().fromJson(
                info.split("id=")[1],
                ChatInfoBean.class);



        ImageLoadUtil.getInstance(this).mImageLoader.displayImage(chatInfiBean.sendName,cvHead);


        StringBuffer sb=new StringBuffer();



        if(SysConfig.uid.equals(chatInfiBean.receiveId))
        {
            sb.append(chatInfiBean.sendName+"邀请你");
            presenter = new ChatPresenter(this, chatInfiBean.sendId, TIMConversationType.C2C);
            presenter.start();
        }else
        {
            sb.append("等待"+ chatInfiBean.sendName);
            presenter = new ChatPresenter(this, chatInfiBean.receiveId, TIMConversationType.C2C);
            presenter.start();

            //如果是发起人 等待点击
            llCheck.setVisibility(View.GONE);
        }



        if (chatInfiBean.chatType == ChatInput.chat_type_voice ) {
            chatType = CHAT_TYPE_VOICE;
            sb.append("进行语音聊天");

        } else if ( chatInfiBean.chatType == ChatInput.chat_type_video ) {
            chatType = CHAT_TYPE_VIDEO;
            sb.append("进行视频聊天");
        }

        tvMsg.setText(sb.toString());


        EventBus.getDefault().register(this);


    }



    @OnClick({R.id.cv_head, R.id.tv_msg, R.id.iv_endcall, R.id.iv_check, R.id.activity_voice_chat_view})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cv_head:
                break;
            case R.id.tv_msg:
                break;
            case R.id.iv_endcall:
//            {
//
//                chatInfiBean.streamType=ChatInput.streamChatClose;
//                String str=ChatInput.streamChatClose+changeString(chatInfiBean);
//                Message message = new TextMessage(str);
//                presenter.sendMessage(message.getMessage());
//            }

                isFinish=false;
                isAnswer=false;
                finish();

                break;
            case R.id.iv_check:
                isAnswer=true;
                Intent intent = null;
//                if (chatType == CHAT_TYPE_VOICE) {
//                    intent = new Intent(ConfirmChatDialogActivity.this, VoiceChatViewActivity.class);
//
//                } else {
//                    intent = new Intent(ConfirmChatDialogActivity.this, VideoChatViewActivity.class);
//                }
//
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.putExtra("chat", info);
//
//            {
//                chatInfiBean.streamType=ChatInput.streamChatAnswer;
//                String str=ChatInput.streamChatAnswer+changeString(chatInfiBean);
//                Message message = new TextMessage(str);
//                presenter.sendMessage(message.getMessage());
//            }
//
//                startActivity(intent);
//
//                finish();




                break;
            case R.id.activity_voice_chat_view:
                break;
        }
    }

    private String changeString(ChatInfoBean chatInfiBean) {
        return new Gson().toJson(chatInfiBean);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(!isFinish)
        {
            if(!isAnswer)
            {
                chatInfiBean.streamType=ChatInput.streamChatClose;
                String str=ChatInput.streamChatClose+changeString(chatInfiBean);
                Message message = new TextMessage(str);
                presenter.sendMessage(message.getMessage());
                presenter.stop();
                presenter=null;
            }
        }



        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showMessage(TIMMessage message) {

    }

    @Override
    public void showMessage(List<TIMMessage> messages) {

    }

    @Override
    public void showRevokeMessage(TIMMessageLocator timMessageLocator) {

    }

    @Override
    public void clearAllMessage() {

    }

    @Override
    public void onSendMessageSuccess(TIMMessage message) {

    }

    @Override
    public void onSendMessageFail(int code, String desc, TIMMessage message) {

    }

    @Override
    public void sendImage() {

    }

    @Override
    public void sendPhoto() {

    }

    @Override
    public void sendText() {

    }

    @Override
    public void sendText(String str) {

    }

    @Override
    public void sendFile() {

    }

    @Override
    public void startSendVoice() {

    }

    @Override
    public void endSendVoice() {

    }

    @Override
    public void sendVideo(String fileName) {

    }

    @Override
    public void cancelSendVoice() {

    }

    @Override
    public void sending() {

    }

    @Override
    public void showDraft(TIMMessageDraft draft) {

    }

    @Override
    public void videoAction() {

    }

    @Override
    public void showToast(String msg) {

    }

    //
}

